package com.syaccp.controller;


import com.syaccp.model.Inspiration;
import com.syaccp.service.InspirationService;
import com.syaccp.utils.AliOSSUtils;
import com.syaccp.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/api/inspiration")
@Api(tags = "灵感")
public class InspirationController {

    @Resource
    private InspirationService inspirationService;
    @Resource
    private AliOSSUtils aliOSSUtils;

    @GetMapping("/selectAll")
    @ApiOperation(value="左边列表")
    public Result selectAll(){

        return Result.success(inspirationService.selectAll());
    }

    @GetMapping("/selectAllTwo")
    @ApiOperation(value="右边列表")
    public Result selectAllTwo(){

        return Result.success(inspirationService.selectAllTwo());
    }

    @PostMapping("/upload")
    @ApiOperation(value="上传图片")
    public Result upload(MultipartFile file) throws IOException {


        String upload = aliOSSUtils.upload(file);


        return Result.success(upload);


    }

    @PostMapping("/add")
    @ApiOperation(value="新建")
    public Result add(@RequestBody Inspiration inspiration) throws IOException {
        System.out.println("inspiration = " + inspiration.getAuthor());
        inspiration.setAuthorImg(inspiration.getImage());
        inspiration.setTime(new Date());

        Integer add = inspirationService.add(inspiration);


        return Result.success(add);


    }

    @GetMapping("/queryOneById")
    @ApiOperation(value="根据id查询inspiration对象")
    public Result queryOneById( Integer id  ) throws IOException {
        System.out.println("ids = " + id);
        Inspiration inspiration = inspirationService.queryOneById(id);

        return Result.success(inspiration);


    }

}
